import { BaseCommand } from '../../core/command.js';
import type { CommandContext } from '../../types/index.js';
import { GitHubPullCommand } from './github-pull.js';

export class DemoCommand extends BaseCommand {
  name = 'demo';
  description = 'Run demo commands to showcase CLI capabilities';

  arguments = [
    {
      name: 'subcommand',
      description: 'Demo subcommand to run',
      required: true,
    },
  ];

  options = [
    {
      name: 'list',
      description: 'List available demo commands',
      type: 'boolean' as const,
      default: false,
    },
  ];

  examples = [
    'genwavejs demo --list',
    'genwavejs demo github-pull user/repo',
  ];

  private subcommands = new Map([
    ['github-pull', new GitHubPullCommand()],
  ]);

  async execute(context: CommandContext): Promise<void> {
    const { list } = context.options;

    if (list) {
      this.listDemoCommands(context);
      return;
    }

    this.validateArguments(context);
    
    const [subcommandName, ...subArgs] = context.args;

    if (!subcommandName) {
      context.logger.error('No demo subcommand specified');
      this.listDemoCommands(context);
      return;
    }

    const subcommand = this.subcommands.get(subcommandName);
    if (!subcommand) {
      context.logger.error(`Unknown demo subcommand: ${subcommandName}`);
      this.listDemoCommands(context);
      return;
    }

    // Create new context for subcommand
    const subContext: CommandContext = {
      ...context,
      args: subArgs,
    };

    try {
      await subcommand.execute(subContext);
    } catch (error) {
      context.logger.error(`Demo command failed: ${subcommandName}`, error);
      throw error;
    }
  }

  private listDemoCommands(context: CommandContext): void {
    context.logger.info('Available demo commands:');
    
    for (const [name, command] of this.subcommands) {
      context.logger.info(`  ${name.padEnd(15)} - ${command.description}`);
    }

    context.logger.info('\nUsage: genwavejs demo <subcommand> [args...]');
    context.logger.info('For help on a specific demo: genwavejs help demo');
  }
}
